import Vue from 'vue'
import VueRouter from 'vue-router'
/* import Login from './components/Login.vue'
import Home from './components/Home.vue'
import Welcome from './components/Welcome.vue'
import Users from './components/user/Users.vue'
import Rights from './components/power/Rights.vue'
import Roles from './components/power/Roles.vue'
import Cate from './components/goods/Cate.vue'
import Params from './components/goods/Params.vue'
import GoodsList from './components/goods/List.vue'
import Add from './components/goods/Add.vue'
import Order from './components/order/Order.vue'
import Report from './components/report/Report.vue'
 */
Vue.use(VueRouter)

const router = new VueRouter({
  routes:[
    {
      //默认进入login页面
      path:'/',
      redirect:'/login'
    },
    {
      path:'/login',
      component:resolve => require(['./components/Login.vue'],resolve)
    },
    {
      path:'/home',
      component:resolve => require(['./components/Home.vue'],resolve),
      redirect:'/Welcome',
      children:[
        {
          path:'/Welcome',
          component:resolve => require(['./components/Welcome.vue'],resolve),
        },
        {
          path:'/users',
          component:resolve => require(['./components/user/Users.vue'],resolve),
        },
        {
          path:'/rights',
          component:resolve => require(['./components/power/Rights.vue'],resolve),
        },
        {
          path:'/roles',
          component:resolve => require(['./components/power/Roles.vue'],resolve),
        },
        {
          path:'/categories',
          component:resolve => require(['./components/goods/Cate.vue'],resolve),
        },
        {
          path:'/params',
          component:resolve => require(['./components/goods/Params.vue'],resolve),
        },
        {
          path:'/goods',
          component:resolve => require(['./components/goods/List.vue'],resolve),
        },
        {
          path:'/goods/add',
          component:resolve => require(['./components/goods/Add.vue'],resolve),
        },
        {
          path:'/orders',
          component:resolve => require(['./components/order/Order.vue'],resolve),
        },
        {
          path:'/reports',
          component:resolve => require(['./components/report/Report.vue'],resolve),
        },
    ]
    },
  ]
})

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  // to 将要访问的路径
  // from 从哪个路径跳转而来
  // next 是一个函数,代表放行
  // next() 放行   next('/login')  强制跳转

  if(to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()
})

export default router